#!/usr/bin/env python
# coding: utf-8

# In[ ]:


#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import urllib
import base64
import json
#client_id 为官网获取的AK， client_secret 为官网获取的SK
client_id = 'z0pmPkifUs3MxMLRLOIFCA75'
client_secret = 'klAXTwvE5dW45jlS7fpRORTx0txFkKfz'
 


#获取token
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    token_content = response.read()
    if token_content:
        token_info = json.loads(token_content)
        token_key = token_info['access_token']
    return token_key



#获取果蔬识别信息
import requests
import base64
import json
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
# 二进制方式打开图片文件
f = open('https://p1.ssl.qhmsg.com/t01e7e776810abc79ca.jpg', 'rb')
img = base64.b64encode(f.read())
params = {"image":img,
          "baike_num":1}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
r=response.json()
r



#取出果蔬识别返回值中关于百度百科内容描述的值
wenben=r['result'][0]['baike_info']['description']
wenben


#将文本转化为语音文件
from aip import AipSpeech
 
 """ 你的 APPID AK SK """
APP_ID = '22977005'
API_KEY = '6SR7iH7kHEHuIAjM1Bt2kjgu'
SECRET_KEY = 'QoiOO4Dqo2pziw0N6KtkMNzFKYdeNKdd'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
 
result = client.synthesis( wenben, 'zh', 1, {
'vol': 5,
})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('audio.mp3', 'wb') as f:
        f.write(result)

